package com.tanhua.server.controller;


import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.holder.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 保存用户信息
     *   UserInfo
     *   请求头中携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo
                                       /*@RequestHeader("Authorization") String token*/){
        //1、解析token
        /*Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer)claims.get("id");*/
        //2、向userinfo中设置用户id

        //  线程里面的方法,更简单
        userInfo.setId(UserHolder.getUserId());
        //3、调用service
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }


    /**
     * 更新用户资料
     */
    @PostMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo
                                         /*@RequestHeader ("Authorization") String token*/){
        /*Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer)claims.get("id");*/
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
